package com.doctor.utils.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.doctor.ui.R;


/**
 * Created by nieyunlong
 * on 2016/8/1.
 */
public abstract class BasePopWindow extends PopupWindow {
    public LayoutInflater inflater;
    public Activity activity;
    /**
     * 是否改变背景
     */
    private boolean isEditBackground = false;

    public BasePopWindow() {

    }

    public BasePopWindow(Activity activity) {
        super(activity);
        this.activity = activity;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * popwindow 属性
     */
    public void initPopwindow(View view) {
        //设置SelectPicPopupWindow的View
        this.setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为透明
//        ColorDrawable dw = new ColorDrawable(0x00000000);

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(new BitmapDrawable());
    }


    /**
     * @param popView 用布局填充器 填充的布局
     * @param view    是pop最外层布局的view 用来计算高度
     */
    public void touchDismiss(View popView, final View view) {
        popView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setOnCance();
            }
        });
    }

    /**
     * 子类继承实现
     */
    public abstract void setOnCance();
}
